import request from '@/utils/request';
import qs from 'qs';
import { TableListParams } from './data.d';

const getRandomuserParams = (params: any) => {
  return {
    pageNo: params.current,
    ...params,
  };
};

//容量单位转换(kb,mb,gb,tb)
function bytesToSize(bytes) {
  if (bytes === 0) return '0 B';
  let k = 1024,
    sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'],
    i = Math.floor(Math.log(bytes) / Math.log(k));

  return (bytes / Math.pow(k, i)).toPrecision(3) + ' ' + sizes[i];
}

//节目列表
export async function programList(params?: TableListParams) {
  params = getRandomuserParams(params);
  const resolution = params.resolution;
  if(resolution && resolution.indexOf("x")>-1){
    const resolutionWidth = resolution.split("x")[0];
    const resolutionHeight = resolution.split("x")[1];
    if(resolutionWidth){
      params["resolutionWidth"] = resolutionWidth;
    }
    if(resolutionHeight){
      params["resolutionHeight"] = resolutionHeight;
    }
  }
  params["resolution"] = undefined;
  const res = request('/api/program/pageList', {
    headers: {
      Authorization: localStorage.getItem('token'),
    },
    params,
  });
  return res.then(function (response) {
    if (response && response.code === 200) {
      //数据转换
      const obj = new Object();
      let list = response.data.list;
      for (var i in list) {
        list[i].resolution = list[i].resolutionWidth + ' x ' + list[i].resolutionHeight;
        list[i].key = list[i].id;
        list[i].totalSize = bytesToSize(parseInt(list[i].totalSize));
      }
      obj['data'] = response.data.list;
      obj['pageCount'] = response.data.pageCount;
      obj['current'] = response.data.pageNo;
      obj['total'] = response.data.totalCount;
      return obj;
    }
  });
}
//节目名称验证
export async function checkedAccount(params: any) {
  const res = request('/api/program/checkNameExists', {
    headers: {
      Authorization: localStorage.getItem('token'),
    },
    params,
  });
  return res;
}

//添加节目
export async function programAdd(params: any) {
  return request('/api/program/add', {
    method: 'POST',
    headers: {
      Authorization: localStorage.getItem('token'),
    },
    data: {
      ...params,
      method: 'update',
    },
  });
}
//节目删除
export async function programRemove(params: any) {
  return request('/api/program/delete/', {
    headers: {
      Authorization: localStorage.getItem('token'),
    },
    method: 'delete',
    params,
  });
}
//批量删除节目
export async function batchDelete(params: any) {
  return request('/api/program/batchDelete/', {
    headers: {
      Authorization: localStorage.getItem('token'),
    },
    method: 'delete',
    params,
  });
}
//节目复制
export async function programCopy(id: number) {
  return request(`/api/program/copy/${id}`, {
    method: 'POST',
    headers: {
      Authorization: localStorage.getItem('token'),
    },
  });
}

//节目详情
export async function programDetail(id: number) {
  return request(`/api/program/getProgramDetail/${id}`, {
    headers: {
      Authorization: localStorage.getItem('token'),
    },
  });
}

//编辑节目【保存】
export async function programUpdate(params: any) {
  return request('/api/program/update', {
    method: 'PUT',
    headers: {
      Authorization: localStorage.getItem('token'),
    },
    data: {
      ...params,
      method: 'update',
    },
  });
}
//分配标签
export async function setTag(params: any) {
  return request('/api/program/setTag', {
    method: 'PUT',
    headers: {
      Authorization: localStorage.getItem('token'),
      'Content-Type': 'application/x-www-form-urlencoded',
    },
    data: qs.stringify(params, { arrayFormat: 'indices', allowDots: true }),
  });
}
